//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        shuaxin: '下拉刷新',
        banner: [],
        CategoryList: [],
        SubjectList: [],
        indicatorDots: false,
        autoplay: true,
        interval: 5000,
        duration: 1000,
    },
    //事件处理函数
    linkTo: function (event) {
        // console.log(event.currentTarget.dataset)
        let linkType = event.currentTarget.dataset.linktype;
        if (linkType == 1) {
            wx.navigateTo({
                url: "../../subPages/home/pages/class/class"
            })
        } else {
            wx.navigateTo({
                url: "../../subPages/home/pages/allLabel/allLabel"
            })
        }
    },
    to_details: function () {
        wx.navigateTo({
            url: "../../subPages/home/pages/details/details"
        })
    },
    to_more: function () {
        wx.navigateTo({
            url: "../../subPages/home/pages/more/more"
        })
    },
    // 首页分类接口
    initCategoryList: function () {
        let that = this;
        let url = app.globalData.url;
        let header = {
            'content-type': 'application/json'
        };
        app.globalData.post(url + '/apiservice/sku/v1/indexCategoryList', header).then((res) => {
            // console.log(res);//正确返回结果
            that.setData({
                CategoryList: res
            })
            wx.stopPullDownRefresh()
            that.setData({
                shuaxin: '下拉刷新'
            })
        }).catch((errMsg) => {
            console.log(errMsg); //错误提示信息
        });
    },
    // 首页banner接口
    initBanner: function () {
        let that = this;
        let url = app.globalData.url;
        let header = {
            'content-type': 'application/json'
        };
        app.globalData.post(url + '/apiservice/sku/v1/indexBannerList', header).then((res) => {
            // console.log(res);//正确返回结果
            that.setData({,
                banner: res
            })
            wx.stopPullDownRefresh()
            that.setData({
                shuaxin: '下拉刷新'
            })
        }).catch((errMsg) => {
            console.log(errMsg); //错误提示信息
        });
    },
    // 首页包装接口
    initSubjectList: function () {
        let that = this;
        let url = app.globalData.url;
        let header = {
            "content-type": "application/x-www-form-urlencoded",
            "device-type": "xiaochengxu-maya",
            "version": "0"
        };
        app.globalData.post(url + '/apiservice/sku/v1/indexSubjectList', header).then((res) => {
            // console.log(res);//正确返回结果
            that.setData({
                SubjectList: res
            })
            wx.stopPullDownRefresh()
            that.setData({
                shuaxin: '下拉刷新'
            })
        }).catch((errMsg) => {
            console.log(errMsg); //错误提示信息
        });
    },
    onLoad: function () {
        this.initBanner()
        this.initCategoryList()
        this.initSubjectList()
    },
    onPullDownRefresh: function () {
        this.setData({
            shuaxin: '正在刷新'
        })
        this.onLoad()
    },
    getUserInfo: function (e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    }
})